#include "logindialog.h"

LoginDialog::LoginDialog(QString *username,QString *password,bool *flag)
{
    //声明标签
    userLabel=new QLabel("用户名");
    passLabel=new QLabel("密码");

    //声明输入框
    userInput=new QLineEdit;
    passInput=new QLineEdit;
    passInput->setEchoMode(QLineEdit::Password);   //设置密码不可显示

    //声明布局
    loginLayout=new QGridLayout();
    buttonLayout=new QHBoxLayout;

    //声明按钮
    confirmButton=new QPushButton(tr("确认"));
    resetButton=new QPushButton(tr("清空"));

    //向按钮布局中添加按钮
    buttonLayout->addWidget(confirmButton);
    buttonLayout->addWidget(resetButton);

    //向本级布局中添加进去控件
    loginLayout->addWidget(userLabel,0,0);
    loginLayout->addWidget(userInput,0,1);
    loginLayout->addWidget(passLabel,1,0);
    loginLayout->addWidget(passInput,1,1);
    loginLayout->addLayout(buttonLayout,2,0,1,2,Qt::AlignCenter);

    //将本级布局添加进入框架布局
    layout->addLayout(loginLayout);

    //保存外界指针
    this->username=username;
    this->password=password;
    this->flag=flag;

    //连接事件与槽
    connect(confirmButton,SIGNAL(clicked(bool)),this,SLOT(pressConfirm()));
    connect(resetButton,SIGNAL(clicked(bool)),this,SLOT(pressReset()));


    *flag=false;//初始化为失败
}

void LoginDialog::pressConfirm()
{
    *username=userInput->text();    //获取用户名
    if(username->isEmpty()||username->isNull())
    {
        QMessageBox::critical(this,tr("错误！"),tr("用户名必须填写！"));
        return;
    }
    *password=passInput->text();    //获取密码
    if(password->isEmpty()||password->isNull())
    {
        QMessageBox::critical(this,tr("错误！"),tr("密码必须填写！"));
        return;
    }
    *flag=true; //表示成功
    close();
}

void LoginDialog::pressReset()
{
    userInput->setText(""); //清空
    passInput->setText(""); //清空
}
